This page last changed on Jul 05, 2006 by alan.cassar.

Mule separates the container in which components are created and the Mule model in which they are managed. This means developers can plug-in a container such as Spring or PicoContainer to construct the components that Mule manages. This section will describe how to do this with the Spring container, though the approach is very similar for other containers.

For our example we are going to use 2 beans; a RestauarantWaiter bean that receives orders, logs the order and then passes it to the KitchenService bean which receives orders and does 'something' with them. Our beans look something like this -

public class RestaurantWaiter
{
    private KitchenService kitchen = null;

    public void takeOrder(Order order) {
	//log order

	//notify kitchen
	this.kitchen.submitOrder(order);
     }

    public void setKitchenService(KitchenService kitchen) {
        this.kitchen = kitchen;
    }

    public KitchenService getKitchenService() {
        return kitchen;
    }
}

Setting up Spring

The first thing to do is define your Spring application context. This context will contain our RestaurantWaiter and KitchenService beans -

<beans>
    <bean id='restaurantWaiter' singleton='false' class='com.foo.RestaurantWaiter'>
        <property name='kitchenService'><ref local='kitchenService'/></property>
    </bean>

    <bean id='kitchenService' class='com.foo.KitchenService'/>
</beans>

So we now have two beans called restaurantWaiter and kitchenService that will be created by Spring. Notice the resturantWaiter bean is not declared as a singleton (by default, all beans in Spring are singletons unless specified otherwise). This is important as Mule will pool your components and telling Spring not to create a singleton ensures that each pooled instance will be a unique instance.

Configuring a Mule Component

Once you have defined the Spring context configuration you can create you reference to myBean in the Mule Xml configuration. Components managed by Mule are defined by using a <mule-descriptor> element. We will create a descriptor that will enable restaurantWaiter to receive events from jms.

<mule-descriptor name="Restaurant Waiter"
     inboundEndpoint="vm://order.queue"
     implementation="restaurantWaiter">
</mule-descriptor>

Whats important here is the implementation attribute; this is used to identify the bean in the container at run-time.

Configuring the Container

The final thing we need to do is tell Mule where to find the Spring application context. the Mule manager has the notion of a container context that can be set to obtain components at runtime and to wire container objects in the Mule Xml configuration. Out of the box, Mule comes with a PicoContainerContext and SpringContainercontext. If these don't suit your needs the UMOContainerContext interface is extremely simple if you want to roll your own. Once the Spring container context is configured on the Mule Manager, Mule will automatically check the value of the mule-descriptor implementation attribute for objects in the container. To configure the Spring Container Context, declare the following within the root Mule Xml configuration.

<mule-configuration>
    ....
    <container-context className='org.mule.extras.spring.SpringContainerContext'>
       <properties>
            <property name='configFile' value='../conf/applicationContext.xml'/>
        </properties>
    </container-context>
    ....
</mule-configuration>

The configFile property specifies the location of the applicationContext.xml file. It is possible to specify a classpath resource or a local file path.

What Happens Next?

Now we are ready to try it out. When the Mule server starts the Container context is also loaded. Each of the mule-descriptor elements are loaded and the implementation attibute is used to look up a bean in the container. The bean is then added to the Mule managed container. When an event is received on the vm://orders.queue, an Order object is passed to the takeOrder() method on the RestaurantWaiter, which then logs the order and passes it to the KitchenService.

Loading dependent objects from the container

As well as using the container context to load a mule-descriptor object implementation, it can be used to resolve dependent objects on any object configured on the Mule server such as endpoints transformer, connectors or agents. To reference a container managed object use the <container-property> element.

<mule-descriptor name='Restaurant Waiter'
     inboundEndpoint='vm://myBean.queue'
     implementation='restaurantWaiter'>
     <properties>
         <container-property name='kitchenService'
             reference='kitchenService' required='true'/>
     </properties>
</mule-descriptor>

Here the <container-property> name attribute refers to the bean mutator method on the managed component 'restaurantWaiter', the reference denotes the bean name in the container and the required attributed determines whether or not an exception is thrown if the reference is not found.

See configuring Properties for more information about configuring Mule objects.

Document generated by Confluence on Nov 27, 2006 10:27